package Utils;

import android.content.Context;
import android.os.Handler;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;


public class JsonUtil {
    public interface OnJsonDown {
        public void ok(String json);

        public void error(String error);
    }
    private static Handler handelr;
    public static void loadJson(Context context, final String path, final OnJsonDown callback){
        if (handelr == null) {
            handelr = new Handler();
        }

        new Thread(){
            @Override
            public void run() {
                super.run();

                HttpUtils httpUtils = new HttpUtils();
                httpUtils.send(HttpRequest.HttpMethod.GET,path,new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> objectResponseInfo) {
                       final String json = objectResponseInfo.result;
                        handelr.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.ok(json);
                            }
                        });

                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        handelr.post(new Runnable() {
                            @Override
                            public void run() {
                                callback.error("网络异常");
                            }
                        });
                    }
                });

            }
        }.start();

    }
}
